package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.YYFMenuManagerService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping("/menuManager")
public class YYFMenuManagerController {
    @Reference
    private YYFMenuManagerService YYFMenuManagerService;



    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        User user1 = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user1.getUsername();

        return YYFMenuManagerService.findPage(queryPageBean,username);
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try{
            YYFMenuManagerService.edit(menu);//发送请求
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findByUsername(Integer id){
        //通过menu的id查询数据并进行回显
        try {
            Menu menu = YYFMenuManagerService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //根据id删除菜单项

    @RequestMapping("/delete")
    public Result delete(Integer id,Integer parentMenuId){
        try{
            YYFMenuManagerService.delete(id,parentMenuId);//发送请求
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }
}
